/*
 * @lc app=leetcode.cn id=696 lang=cpp
 *
 * [696] 计数二进制子串
 */

#include <limits.h>

#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

// @lc code=start
class Solution {
   public:
    int countBinarySubstrings(string s) {
        int ans = 0;
        int count = 0;
        char lastChar = '0';
        vector<int> counter;

        // 遍历原数组，整合相邻的相同元素
        for (char c : s) {
            if (c == lastChar) {
                count++;
            } else {
                lastChar = c;
                counter.push_back(count);
                count = 1;
            }
        }
        counter.push_back(count);

        // 遍历统计数组，计算总数
        for (int i = 0; i < counter.size() - 1; i++) {
            ans += min(counter[i], counter[i + 1]);
        }

        return ans;
    }
};
// @lc code=end
